package com.gzbsoft.sc.modules.sys.entity;
import com.fasterxml.jackson.annotation.JsonBackReference;
import io.ebean.annotation.DbComment;
import io.ebean.annotation.DocStore;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;
@DocStore
@Entity
@Data
@Table(name = "sys_role")
@Builder
@DbComment("系统角色")
public class SysRole extends BaseEntity {
    @JsonBackReference
    @ManyToOne
    @JoinColumn(name = "sys_user_id")
    private SysUser sysUser;
    @NotBlank(message = "角色名称不能为空")
    private String name;
    @ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(name = "ref_sys_role_menu",
            joinColumns = {@JoinColumn(name = "role_id")},
            inverseJoinColumns = {@JoinColumn(name = "menu_id")}
    )
    private List<SysMenu> menuList;
    @Override
    public String toString() {
        return "SysRole{" +
                "name='" + name + '\'' +
                ", menuList=" + menuList +
                ", id=" + id +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        SysRole sysRole = (SysRole) o;
        return name.equals(sysRole.name) &&
                Objects.equals(menuList, sysRole.menuList);
    }
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), name, menuList);
    }
}
